<template>
  <div class="goods-comment">
    <div class="head" v-if="conditions">
      <div class="data">
        <p>
          <span>{{ conditions.salesCount }}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{ conditions.praisePercent }}</span
          ><span>好评率</span>
        </p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a
            @click="toggle(index)"
            :class="{ active: currentIndex === index }"
            v-for="(item, index) in conditions.tags"
            :key="index"
            href="javascript:;"
            >{{ item.title }}（{{ item.tagCount }}）</a
          >
        </div>
      </div>
    </div>
    <div class="sort">
      <span>排序：</span>
      <a :class="{ active: reqParams.sortField === null }" @click="changeSort(null)" href="javascript:;">默认</a>
      <a :class="{ active: reqParams.sortField === 'createTime' }" @click="changeSort('createTime')" href="javascript:;"
        >最新</a
      >
      <a
        :class="{ active: reqParams.sortField === 'praiseCount' }"
        @click="changeSort('praiseCount')"
        href="javascript:;"
        >最热</a
      >
    </div>
    <!-- 评论列表 -->
    <div class="list">
      <div class="item" v-for="item in list" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar" alt="" />
          <span>{{ formatNickname(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i v-for="star in item.score" :key="star" class="iconfont icon-wjx01"></i>
            <i v-for="unstar in 5 - item.score" :key="unstar" class="iconfont icon-wjx02"></i>
            <span class="attr">{{ formatSpec(item.orderInfo.specs) }}</span>
          </div>
          <div class="text">{{ item.content }}</div>
          <!-- 评价图片预览 -->
          <GoodsCommentImage :pictures="item.pictures" />
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan"><i class="iconfont icon-dianzan"></i>{{ item.praiseCount }}</span>
          </div>
        </div>
      </div>
    </div>
    <XtxPagination @change-page="changePage" />
  </div>
</template>
<script>
import { ref, inject, reactive, watch } from 'vue'
import { findCommentInfoByGoods, findCommentListByGoods } from '@/api/product.js'
import GoodsCommentImage from './goods-comment-image.vue'

export default {
  name: 'GoodsComment',
  components: { GoodsCommentImage },
  setup() {
    const conditions = ref(null)
    const goods = inject('goods')
    // 获取筛选条件的数据
    findCommentInfoByGoods(goods.value.id).then(ret => {
      ret.result.tags.unshift({
        title: '有图',
        type: 'pic',
        tagCount: ret.result.hasPictureCount
      })
      ret.result.tags.unshift({
        title: '全部评价',
        type: 'all',
        tagCount: ret.result.evaluateCount
      })
      conditions.value = ret.result
    })
    // 当前选中的条件的索引
    const currentIndex = ref(0)
    const toggle = index => {
      currentIndex.value = index
      // 根据索引，获取当前点击的标签的对象信息
      const info = conditions.value.tags[index]
      if (info.type === 'pic') {
        // 点击有图
        reqParams.hasPicture = true
        reqParams.tag = null
      } else if (info.type === 'all') {
        // 点击全部评价
        reqParams.hasPicture = null
        reqParams.tag = null
      } else {
        // 点击其他四个标签之一
        reqParams.hasPicture = null
        reqParams.tag = info.title
      }
      // 每次修改条件，重置页码
      reqParams.page = 1
    }
    // 获取评论列表数据
    const list = ref([])
    const total = ref(0)
    // 筛选条件准备
    const reqParams = reactive({
      // 当前页码
      page: 1,
      // 每页的条数
      pageSize: 10,
      // 是否有图片
      hasPicture: null,
      // 筛选条件
      tag: null,
      // 排序的字段
      sortField: null
    })
    // 侦听参数的变化
    watch(
      reqParams,
      () => {
        findCommentListByGoods(goods.value.id, reqParams).then(ret => {
          total.value = ret.result.counts
          list.value = ret.result.items
        })
      },
      {
        immediate: true
      }
    )
    // 控制排序条件的切换
    const changeSort = type => {
      console.log(type)
      reqParams.sortField = type
      reqParams.page = 1
    }
    // 格式化规格信息
    const formatSpec = specs => {
      return specs.reduce((ret, item) => ret + item.name + ':' + item.nameValue + ' ', '')
    }
    // 格式化昵称
    const formatNickname = nickname => {
      return nickname.substr(0, 1) + '****' + nickname.substr(-1)
    }
    const changePage = page => {
      reqParams.page = page
    }
    return {
      formatNickname,
      formatSpec,
      list,
      conditions,
      currentIndex,
      toggle,
      reqParams,
      changeSort,
      total,
      changePage
    }
  }
}
</script>
<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
  .list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }
          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }
      .text {
        color: #666;
        line-height: 24px;
      }
      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
}
</style>
